package com.xujl.rxtask.dualtest;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.xujl.rxtask.R;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class DualTestActivity extends AppCompatActivity {
    private static final String TAG = "DualTestActivity";
    long count1;
    int testCount = 10000;
    TextView tv1;

    @Override
    protected void onCreate (@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dual_test);
        final long currentTimeMillis = System.currentTimeMillis();
        tv1 = findViewById(R.id.tv);
        Log.i(TAG, "onCreate: " + (System.currentTimeMillis() - currentTimeMillis));
        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick (View v) {
                count1 = System.currentTimeMillis();
                final Intent intent = new Intent(DualTestActivity.this, DualTestActivity1.class);
                intent.putExtra("count", testCount);
                startActivityForResult(intent, 6);
            }
        });
        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick (View v) {
                count1 = System.currentTimeMillis();
                final Intent intent = new Intent(DualTestActivity.this, DualTestActivity2.class);
                intent.putExtra("count", testCount);
                startActivityForResult(intent, 6);
            }
        });
        EditText et = findViewById(R.id.et);
        et.setText(testCount + "");
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged (CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged (CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged (Editable s) {
                if (s != null && !TextUtils.isEmpty(s.toString())) {
                    testCount = Integer.valueOf(s.toString());
                }
            }
        });
    }

    @Override
    protected void onActivityResult (int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            return;
        }
        final long time = data.getLongExtra("time", 0);
        tv1.setText((time - count1) + "毫秒");
    }
}
